package com.example.page;

import com.example.dao.User;
import com.example.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author 22044133 左立志
 * @data 2025/3/6 15:06
 */
public class TestPage {
    @Test
    public void SelectUser(){
        SqlSession sqlSession=null;
        InputStream is=null;;
        try {
            is = Resources.getResourceAsStream("MybatisConfig.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            sqlSession = sqlSessionFactory.openSession(true);
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            PageHelper.startPage(1,5);
            List<User> users = mapper.selectAll();
            for(User u:users){
                System.out.println(u);
            }
            PageInfo<User> userPageInfo = new PageInfo<>(users);
            System.out.println("总条数："+userPageInfo.getTotal());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if(sqlSession != null){
                sqlSession.close();
            }
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
